package com.brillilab.service.logic.kit;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.brillilab.domain.enums.kit.KitEnum.Classify;
import com.brillilab.domain.pa.kit.KitSpecAddPa;
import com.brillilab.domain.po.global.GlobalKit;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.vo.kit.ReagentDetailsVo;
import com.brillilab.domain.vo.kit.ReagentKitSpecVo;
import com.brillilab.domain.vo.kit.SampleInfoVo;
import com.brillilab.domain.vo.kit.SampleSpecVo;
import com.brillilab.service.core.global.IGlobalKitService;
import com.brillilab.service.core.kit.IKitService;

/**
 * 搜索导入Logic
 * 
 * @author lj
 *
 */
@Service
@Transactional
public class KitSearchLogic {
	@Autowired
	private IGlobalKitService globalKitService;
	@Autowired
	private IKitService kitService;
	@Autowired
	private SampleLogic sampleLogic;
	@Autowired
	private ReagentLogic reagentLogic;
	@Autowired
	private GlobalKitLogic globalKitLogic;

	/**
	 * 搜索试剂列表
	 * @param labId
	 * @param itemNo
	 * @return
	 */
	public List<ReagentDetailsVo> searchReagentList(Long labId, String itemNo) {
		List<ReagentDetailsVo> voList = new ArrayList<>();
		List<GlobalKit> globalKitList = globalKitService.selectList(Classify.REAGENT.getValue(), itemNo);
		globalKitList.forEach(globalKit -> {
			// 根据fromId判断全局试剂是否有导入过试剂库，以导入过显示导入后的试剂
			List<Kit> kitList = kitService.selectListByFromId(labId, globalKit.getId());
			if (!CollectionUtils.isEmpty(kitList)) {
				kitList.forEach(kit -> {	
					ReagentDetailsVo detailsVo = reagentLogic.getReagentDetails(kit.getId());
					if(!CollectionUtils.isEmpty(detailsVo.getReagentList())) {
						voList.add(detailsVo);
					}
				});
			}else{
				ReagentDetailsVo detailsVo = globalKitLogic.getReagentDetails(globalKit.getId());
				if(!CollectionUtils.isEmpty(detailsVo.getReagentList())) {
					voList.add(detailsVo);
				}
			}
		});
	
		return voList;
	}

	/**
	 * 获取试剂规格信息列表
	 * 
	 * @param isLabExist
	 *            是否实验室已有：0-否，1-是
	 * @param kitId
	 *            kitD
	 * @return
	 */
	public List<ReagentKitSpecVo> getReagentSpec(Integer isLabExist, Long kitId) {
		if (isLabExist == 1) {//实验室试剂
			return reagentLogic.getReagentSpec(kitId);
		} else {//全局试剂
			return globalKitLogic.getReagentSpec(kitId);
		}
	}
	
	
	/**
	 * 新增试剂规格表
	 * 
	 * @param pa
	 * @return
	 */
	public Long addReagentSpec(KitSpecAddPa pa) {
		if (pa.getIsLabExist() == 1) {// 实验室试剂
			return reagentLogic.addReagentSpec(pa);
		} else {// 全局试剂
			return globalKitLogic.addReagentSpec(pa);
		}
	}

	/**
	 * 导入为新试剂
	 * 
	 * @param labId
	 * @param globalKitId
	 * @return
	 */
	public Long reagentImport(Long labId, Long globalKitId, Long creatorId) {
		return globalKitLogic.reagentImport(labId, globalKitId, creatorId);
	}


	// ==========================================样品相关接口=======================================//
	
	/**
	 * 搜索样品列表
	 * @param labId
	 * @param itemNo
	 * @return
	 */
	public List<SampleInfoVo> searchSampleList(Long labId, String itemNo) {
		List<SampleInfoVo> voList = new ArrayList<>();
		List<GlobalKit> globalKitList = globalKitService.selectList(Classify.SAMPLE.getValue(), itemNo);
		globalKitList.forEach(globalKit -> {
			// 根据fromId判断全局试剂是否有导入过试剂库，以导入过显示导入后的试剂
			List<Kit> kitList = kitService.selectListByFromId(labId, globalKit.getId());
			if (kitList != null && kitList.size() > 0) {
				kitList.forEach(kit -> {	
					voList.add(sampleLogic.getSampleInfo(kit.getId(),null));
				});
			}else{
				voList.add(globalKitLogic.getSampleDetails(globalKit.getId()));
			}
		});
		return voList;
	}
	
	
 
	/**
	 * 样品导入
	 * 
	 * @param labId
	 * @param globalKitId
	 * @return
	 */
	public SampleSpecVo sampleImport(Long labId, Long globalKitId, Long creatorId) {
		return globalKitLogic.sampleImport(labId, globalKitId, creatorId);
	}

}
